package com.ericsson.rm.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ericsson.rm.form.InsertResourceCommand;
import com.ericsson.rm.response.EmployeeStatusInDBResponse;
import com.ericsson.rm.response.ProjectDetailsResponse;
import com.ericsson.rm.response.ProjectResourceDetailResponse;
import com.ericsson.rm.service.ResourceAllocateService;
import com.ericsson.rm.util.Utility;
import com.ericsson.rm.vo.ProjectResourceDetailVO;

@Controller
public class ResourceAllocationController {
	private final static Logger logger = LoggerFactory.getLogger(ResourceAllocationController.class);
	private final static String CLASS_NAME = ResourceAllocationController.class.getSimpleName();
	private final static String PAGE_INSERT_RESOURCE_FORM = "allocateResource";
	
	@Autowired
	ResourceAllocateService resourceAllocateService;
	
	public ResourceAllocateService getResourceAllocateService() {
		return resourceAllocateService;
	}

	public void setResourceAllocateService(
			ResourceAllocateService resourceAllocateService) {
		this.resourceAllocateService = resourceAllocateService;
	}

	@RequestMapping(value="/", method = RequestMethod.GET)
	public String get()
	{
		return "";
	}


	@RequestMapping(value ="/allocateResource", method = RequestMethod.GET)
	public String initForm(ModelMap model)
	{
		final String METHOD_NAME = CLASS_NAME + ".initForm() ";
		System.out.println("Init method of allocation controller......................................");
		if(logger.isDebugEnabled()){
			logger.debug(METHOD_NAME+"entering...");
		}
		
		//InsertResourceCommand insertResourceCmd = new InsertResourceCommand();
		//model.addAttribute("InsertResourceCommand",insertResourceCmd);
		return PAGE_INSERT_RESOURCE_FORM;
	}
	
	
	
	@RequestMapping(value="/checkEmployee/{id}", method = RequestMethod.GET)
	public @ResponseBody EmployeeStatusInDBResponse checkEmployeeInDB(@PathVariable("id") String empID)
	{
		final String METHOD_NAME = CLASS_NAME + ".checkEmplyeeInDB()";
		System.out.println("Sending Json request..........."+METHOD_NAME);
		if(logger.isDebugEnabled())
		{
			logger.debug(METHOD_NAME+"entering...........");
		}
		return resourceAllocateService.checkEmpIDfromTable(empID);
	}
	
	@RequestMapping(value="/displayProjs/{projName}", method = RequestMethod.GET)
	public @ResponseBody List<String> getProjectFromDB(@PathVariable("projName") String projName)
	{
		final String METHOD_NAME = CLASS_NAME + ". getProjectNameFromDB()";
		System.out.println("Sending JSON request from "+METHOD_NAME+" ................");
		if(logger.isDebugEnabled())
		{
			logger.debug(METHOD_NAME+"entering--------------");
		}
		return resourceAllocateService.getProjNameFromTable(projName);
	}
	
	@RequestMapping(value="/displayProjNames/{projName}", method = RequestMethod.GET)
	public @ResponseBody List<ProjectDetailsResponse> getProjectDetailFromDB(@PathVariable("projName") String projName)
	{
		final String METHOD_NAME = CLASS_NAME + ". getProjectNameFromDB()";
		System.out.println("Sending JSON request from "+METHOD_NAME+" ................");
		if(logger.isDebugEnabled())
		{
			logger.debug(METHOD_NAME+"entering--------------");
		}
		return resourceAllocateService.getProjectDetailFromTable(projName);
	}
	
	@RequestMapping(value="/displayProjResourceDetailTable/{projId}", method = RequestMethod.GET)
	public @ResponseBody List<ProjectResourceDetailResponse> getProjectResourceDetailFromDB(@PathVariable("projId") String projId)
	{
		final String METHOD_NAME = CLASS_NAME + ".getProjectResourceDetailFromDB()";
		System.out.println("Sending JSON request from "+METHOD_NAME+"...........................");
		if(logger.isDebugEnabled())
		{
			logger.debug(METHOD_NAME+"entering.........................");
		}
		/*ProjectResourceDetailVO projectResourceDetailVO = new ProjectResourceDetailVO();
		ProjectResourceDetailResponse projectResourceDetailResponse = (ProjectResourceDetailResponse)Utility.copyBean(projectResourceDetailVO, ProjectResourceDetailVO.class);
		List<ProjectResourceDetailResponse> projresourcelist = resourceAllocateService.getProjectResourceDetailFromTable(projId);*/
		
		
		return resourceAllocateService.getProjectResourceDetailFromTable(projId);
	}
	
}
